`
https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxAncestorDiff = function (root) {
  let res = 0

  const traverse = (root) => {
    // base case
    if (root === null) {
      return [Infinity, -Infinity]
    }

    const leftMinMax = traverse(root.left)
    const rightMinMax = traverse(root.right)

    // 获取以当前结点作为根节点的树里面的最大值和最小值
    const minVal = Math.min(root.val, leftMinMax[0], rightMinMax[0])
    const maxVal = Math.max(root.val, leftMinMax[1], rightMinMax[1])

    // 更新最大差值
    res = Math.max(res, Math.abs(root.val - minVal), Math.abs(root.val - maxVal))

    return [minVal, maxVal]
  }

  traverse(root)

  return res
};